package com.link.speechdemo.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;
import java.io.Serializable;

/**
 * Created by Administrator on 2016/11/16 0016.
 */

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;
    private boolean looper;
    private UpdateThread update = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        looper = true;
        update = new UpdateThread();
        mediaPlayer.setOnPreparedListener(new MyOnPreparedListener());
        update.start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBind();
    }

    class MyOnPreparedListener implements MediaPlayer.OnPreparedListener{

        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
            Intent intent = new Intent("music_start");
            sendBroadcast(intent);
        }
    }

    class MyBind extends Binder implements IMusicPlayer {

        @Override
        public void playOrPause() {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            } else {
                mediaPlayer.start();
            }
        }

        @Override
        public void playCurrentMusic(String url) {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void seekTo(int position) {
            mediaPlayer.seekTo(position);
        }
    }

    class UpdateThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (looper) {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (mediaPlayer.isPlaying()) {
                    int dura = mediaPlayer.getDuration();
                    int current = mediaPlayer.getCurrentPosition();
                    Intent in = new Intent("update");
                    in.putExtra("duration", dura);
                    in.putExtra("current", current);
                    sendBroadcast(in);
                }
            }
        }
    }

    private void stopUpdate() {
        update = null;
    }

    @Override
    public void onDestroy() {
        //释放mediaPlayer
        mediaPlayer.release();
        looper = false;
        stopUpdate();
        super.onDestroy();
    }

    private interface IMusicPlayer extends Serializable {
        void playOrPause();

        void playCurrentMusic(String url);

        void seekTo(int position);
    }

}
